.syntax unified
.text
.thumb

.extern new_tcb
.extern old_tcb
.extern tcb_list

.global PendSV_Handler
.thumb_func
PendSV_Handler:
    CPSID   I
    MRS     R0,PSP
    CBZ     R0,Skip     /*检测是否是初次启动*/
    /*保存R4-R11寄存器*/
    SUBS    R0,R0,#0x20
    STM     R0,{R4-R11}
    /*更新PSP地址*/
    LDR     R1,=old_tcb
    LDR     R1,[R1]
    LDR     R2,=tcb_list
    MOV     R3,#0x5
    MUL     R1,R1,R3
    ADDS    R2,R2,R1
    STR     R0,[R2]
Skip:
    /*切换至新任务堆栈指针*/
    LDR     R1,=new_tcb
    LDR     R1,[R1]
    LDR     R2,=tcb_list
    MOV     R3,#0x5
    MUL     R1,R1,R3
    ADDS    R2,R2,R1   
    LDR     R0,[R2]
    LDM     R0,{R4-R11}
    ADDS    R0,R0,#0x20
    MSR     PSP,R0
    ORR     LR,LR,#0x04
    CPSIE   I
    BX      LR
